/*
        Copyright 2012 Google Inc. All Rights Reserved.

        Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this file except in compliance with the License.
        You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

        Unless required by applicable law or agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License.package main
*/

package gominionbase

import (
	"time"
	"math/rand"
)

type CardType uint

// Card type constants
const (
	EMPTY CardType = iota
	ACTION
	ACTION_ATTACK
	ACTION_REACTION
	CURSE
	TREASURE
	VICTORY
)

func init() {
	rand.Seed(time.Now().Unix())
}

// Interface for generic cards
type Card interface {
	Type() CardType // card type
	Name() string // card name
	Cost() int // cost to purchase
	Actions() int // additonal actions when this card is played
	Buys() int // additional buys when this card is played
	Cards() int // additional cards when this card is played
	Money() int // additional money when this card is played
        VictoryPoints() int // victory points this card gives *at the end of the game only*
        SpecialRules() // any special rules for this card
}

// CardStack struct for decks and piles
type CardStack struct {
	cards []Card
	top int
}

func (cs *CardStack) Pop() Card {
	if ( cs.top == 0 ) {
		return nil
	}
	cs.top--
	return cs.cards[cs.top]
}

func (cs *CardStack) Push(new_card Card) {
	if len(cs.cards) > cs.top {
		cs.cards[cs.top] = new_card
	} else {
		cs.cards = append(cs.cards,new_card)
	}
	cs.top++
}

func (cs *CardStack) IsEmpty() bool {
	if cs.top == 0 {
		return true
	}
	return false
}

func (cs *CardStack) EmptyStack() {
	cs.top = 0
}

// Fisher-Yates shuffle
func (cs *CardStack) Shuffle() {
	var tmp Card
	for i := cs.top-1 ; i > 0 ; i-- {
		tmp = cs.cards[i]
		j := rand.Int31n(int32(i)+1)
		cs.cards[i] = cs.cards[j]
		cs.cards[j] = tmp
	}
}

// CardHand mod of the CardStack.
type CardHand struct {
	CardStack
}

func (ch *CardHand) Card(index int) Card {
	if ( index >= ch.top ) {
		return nil
	}
	return ch.cards[index]
}

func (ch *CardHand) RemoveCard(index int) Card {
	ch.top--
	tmp := ch.cards[index]
	ch.cards[index] = ch.cards[ch.top]
	return tmp
}

func (ch CardHand) Hand() []Card {
	return ch.cards[:ch.top]
}

// An empty card which implements default methods for struct embedding.
type EmptyCard struct {
}

func (c EmptyCard) Type() CardType {
	return EMPTY
}

func (c EmptyCard) Name() string {
	return ""
}

func (c EmptyCard) Cost() int {
	return 0
}

func (c EmptyCard) Actions() int {
	return 0
}

func (c EmptyCard) Buys() int {
	return 0
}

func (c EmptyCard) Cards() int {
	return 0
}

func (c EmptyCard) Money() int {
	return 0
}

func (c EmptyCard) VictoryPoints() int {
	return 0
}

func (c EmptyCard) SpecialRules() {
}
